package com.own.business.creditbank.model;

import com.own.business.creditbank.function.ResolveFunction;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * ResultBaseVo
 *
 * @author chenxueli
 * @date 2022-08-05 11:16:00
 */
@Schema(title = "学分银行数据响应对象（标准模式）")
@Getter
@Setter
@Accessors(chain = true)
public class ResultBaseVo {

    @Schema(title = "响应code")
    private Integer code;

    @Schema(title = "响应消息")
    private String message;

    @Schema(title = "响应数据")
    private String data;

    @Schema(title = "是否成功")
    private Boolean success;

    public <T> ResultVo<T> resolve() {
        var vo = new ResultVo<T>();
        vo.setCode(this.getCode());
        vo.setMessage(this.getMessage());
        vo.setData(null);
        vo.setSuccess(this.getSuccess());
        return vo;
    }

    public <T> ResultVo<T> resolve(ResolveFunction<T> resolveFunction) {
        var vo = new ResultVo<T>();
        vo.setCode(this.getCode());
        vo.setMessage(this.getMessage());
        vo.setData(this.getData() == null ? null : resolveFunction.resolve(this.getData()));
        vo.setSuccess(this.getSuccess());
        return vo;
    }

}
